package com.yiwei.hdb.http;

import android.graphics.Bitmap;
import android.support.v4.util.LruCache;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
import com.yiwei.hdb.activity.Application;

/**
 * volley单例工具类
 * Created by Master.Dong on 2016/4/7 0007.
 */
public class VolleySingleton {
//    private static VolleySingleton volleySingleton;
    private static RequestQueue mRequestQueue;
    private static ImageLoader mImageLoader;
    static {
        mRequestQueue = getRequestQueue();
        mImageLoader = new ImageLoader(mRequestQueue,
                new ImageLoader.ImageCache() {
                    private final LruCache<String, Bitmap> cache = new LruCache<String, Bitmap>(20);

                    @Override
                    public Bitmap getBitmap(String url) {
                        return cache.get(url);
                    }

                    @Override
                    public void putBitmap(String url, Bitmap bitmap) {
                        cache.put(url, bitmap);
                    }
                });
    }




//    private static Context mContext;

    public VolleySingleton() {
        mRequestQueue = Volley.newRequestQueue(Application.getInstance());
        mImageLoader = new ImageLoader(mRequestQueue,
                new ImageLoader.ImageCache() {
                    private final LruCache<String, Bitmap> cache = new LruCache<String, Bitmap>(20);

                    @Override
                    public Bitmap getBitmap(String url) {
                        return cache.get(url);
                    }

                    @Override
                    public void putBitmap(String url, Bitmap bitmap) {
                        cache.put(url, bitmap);
                    }
                });
    }

//    public static synchronized VolleySingleton getVolleySingleton() {
//        if (volleySingleton == null) {
//            volleySingleton = new VolleySingleton();
//        }
//        return volleySingleton;
//    }

    public static RequestQueue getRequestQueue() {
        if (mRequestQueue == null) {
            mRequestQueue = Volley.newRequestQueue(Application.getInstance());
        }
        return mRequestQueue;
    }

    public <T> void addToRequestQueue(Request<T> req) {
        getRequestQueue().add(req);
    }

    public static ImageLoader getImageLoader() {
        return mImageLoader;
    }
}
